﻿using HotelManager.Common;
using HotelManager.Service;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore;
using Prism.Ioc;
using Prism.Regions;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using LiveChartsCore.SkiaSharpView.Painting.Effects;
using LiveChartsCore.SkiaSharpView.Extensions;
using LiveChartsCore.VisualElements;
using LiveChartsCore.SkiaSharpView.VisualElements;

namespace HotelManager.ViewModels
{
    public class TotalViewModel : NavigationViewModel
    {
        private readonly IDialogHostService dialog;
        private readonly IRegionManager regionManager;
        private readonly ITypeService typeService;
        public TotalViewModel(IDialogHostService dialog, IContainerProvider provider, ITypeService _typeService) : base(provider)
        {
            regionManager = provider.Resolve<IRegionManager>();
            this.dialog = dialog;
            this.typeService = _typeService;
            LiveCharts.Configure(config => config.HasGlobalSKTypeface(SKFontManager.Default.MatchCharacter('汉')));
        }
        private ISeries[] series;
        public ISeries[] Series
        {
            get { return series; }
            set { series = value; RaisePropertyChanged(); }
        }
        private LabelVisual title;
        public LabelVisual Title
        {
            get { return title; }
            set { title = value; RaisePropertyChanged(); }
        }
        private Axis[] xAxes;
        public Axis[] XAxes
        {
            get { return xAxes; }
            set { xAxes = value; RaisePropertyChanged(); }
        }
        private Axis[] yAxes;
        public Axis[] YAxes
        {
            get { return yAxes; }
            set { yAxes = value; RaisePropertyChanged(); }
        }
        void GetDataAsync()
        {
            UpdateLoading(true);

            var memoResult = typeService.GetRoomTypeTotalMoney();
            if (memoResult.Any())
            {
                ////RoomTypeLists.Clear();
                //foreach (var item in memoResult)
                //{
                //    //RoomTypeLists.Add(item);
                //}
                
                Series = new ISeries[]
                {
                    new ColumnSeries<double>
                    {
                        Values=memoResult.Select(o=>(double)o.totalMoney).ToArray()
                    }
                };
                XAxes = new Axis[]
                {
                    new Axis
                    {
                        Name="房间类型销售额统计",
                        Labels=memoResult.Select(o=>o.roomtypeName).ToArray(),
                        LabelsPaint=new SolidColorPaint(SKColors.Black)
                    }
                };
            }
            else
            {
                Series = new ISeries[]
                {
                    new ColumnSeries<double>
                    {
                        Values=new double[]{426,583,104}
                    }
                };
                XAxes = new Axis[]
                {
                    new Axis
                    {
                        Name="Salesman/woman",
                        Labels=new string[]{"王","赵","张"},
                        LabelsPaint=new SolidColorPaint(SKColors.Black)
                    }
                };
            }
            //Series = new ISeries[]
            //{
            //    new ColumnSeries<double>
            //    {
            //        Values=new double[]{426,583,104}
            //    }
            //};
            Title = new LabelVisual
            {
                Text = "My chart title",
                TextSize = 25,
                Padding = new LiveChartsCore.Drawing.Padding(15),
                Paint = new SolidColorPaint(SKColors.DarkSlateGray)
            };
            //XAxes = new Axis[]
            //{
            //    new Axis
            //    {
            //        Name="Salesman/woman",
            //        Labels=new string[]{"王","赵","张"},
            //        LabelsPaint=new SolidColorPaint(SKColors.Black)
            //    }
            //};
            YAxes = new Axis[]
            {
                new Axis
                {
                    Name="Sales amount",
                    NamePadding=new LiveChartsCore.Drawing.Padding(0,15),
                    Labeler=Labelers.Currency,
                    LabelsPaint=new SolidColorPaint
                    {
                        Color=SKColors.Blue,
                        FontFamily="Time New Roman",
                        SKFontStyle=new SKFontStyle(SKFontStyleWeight.ExtraBold,SKFontStyleWidth.Normal,SKFontStyleSlant.Italic)
                    },
                }
            };
            UpdateLoading(false);
        }
        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);
            GetDataAsync();
        }
    }
}
